/*
This file produces the figures for Sandvik, Sauoma, Seegert, Stanton (2020)

INPUTS
     data
		a) DataforAnalysis---created in MergeData.do
		
OUTPUTS
	figures
		1) figure 1  dispersion in log revenue-per-call
		2) figure 2  illustration of experimental design and allocation to treatments  [not produced in this file]
		3) figure 3  mean revenue-per-call by treatment during the pre-intervention and intervention periods
		4) figure 4  the evolution of revenue-per-call over time, by treatment group
		5) figure 5  dispersion in log revenue-per-call in the post-intervention period




*/


clear
set more off

*set paths 
	cd  
	global datapath  
	global tablepath  
	global figurepath  
    global logpath

use "$datapath\DataforAnalysis.dta", clear
 

cap log close
log using "$logpath\produceFigures.smcl", replace


*------------------------------------------------------------------------------*
* Figure 1: dispersion in log revenue-per-call
*------------------------------------------------------------------------------*
	egen bgwgroup = group(brand Week)
	gen Ten = tenure*365/7 // Turns into weeks of tenure.


	areg lrpc i.bgwgroup if  Treatment!=5 & Week<1, absorb(ID)
	predict res, dres	
	replace res = . if !e(sample)
	replace res = res+_b[_cons]
	xtmixed res || ID: , reml
	rename res res_disp_Pre
	predict dfe, reffects
	replace dfe = . if ~e(sample)
	capture drop res

	rename dfe worker_fx_baseline


	matrix IQR_pre = J(7,2,.)

	sort ID Week
	by ID: gen use_pre = _n==1 if Week<=0
	sum worker_fx_baseline if Week==0 & Treatment!=5, d
	disp r(p75)-r(p25)
	matrix IQR_pre[1,1] = r(p75)-r(p25)
	sum worker_fx_baseline if Week<=0 & Treatment!=5, d
	matrix IQR_pre[1,2] = r(p75)-r(p25)

	sum Ten if Week==0 & Treatment!=5, d
	sum worker_fx_baseline if Week==0 & Ten>=r(p50) & Treatment!=5, d
	disp r(p75)-r(p25)
	matrix IQR_pre[2,1] = r(p75)-r(p25)
	sum Ten if Week<=0 & Treatment!=5, d
	sum worker_fx_baseline if Week<=0 & Ten>=r(p50) & Treatment!=5, d
	matrix IQR_pre[2,2] = r(p75)-r(p25)


	sum lrpc if Week==0 & Treatment!=5, d
	matrix IQR_pre[3,1] = r(p75)-r(p25)
	sum lrpc if Week<=0 & Treatment!=5, d
	matrix IQR_pre[3,2] = r(p75)-r(p25)

	sum Ten if Week==0 & Treatment!=5, d
	sum lrpc if Week==0 & Treatment!=5 & Ten>=r(p50), d
	matrix IQR_pre[4,1] = r(p75)-r(p25)
	sum Ten if Week<=0 & Treatment!=5, d
	sum lrpc if Week<=0 & Treatment!=5 & Ten>=r(p50), d
	matrix IQR_pre[4,2] = r(p75)-r(p25)

	sum lrpc if Week==0
	matrix IQR_pre[5,1] = r(sd)
	sum lrpc if Week<=0
	matrix IQR_pre[5,2] = r(sd)

	sum worker_fx_baseline if Week==0
	matrix IQR_pre[6,1] = r(sd)
	sum worker_fx_baseline if Week<=0
	matrix IQR_pre[6,2] = r(sd)

	sum Ten if Week==0 & Treatment!=5, d
	sum worker_fx_baseline if Week==0 & Ten>=r(p50) & Treatment!=5, d
	matrix IQR_pre[7,1] = r(sd)
	sum Ten if Week<=0 & Treatment!=5, d
	sum worker_fx_baseline if Week<=0 & Ten>=r(p50) & Treatment!=5, d
	matrix IQR_pre[7,2] = r(sd)

	sum lrpc
	replace worker_fx_baseline = worker_fx_baseline+r(mean)


	matrix rownames IQR_pre = "Baseline"  "HighTen Baseline" "Raw" "Raw HighTen" "Raw SD" "Worker FX SD" "High Ten Worker FX SD"

	mat2txt , matrix(IQR_pre) saving( "$tablepath\IQR_PrePeriod_Summary.tex") replace

	sum lrpc, d
	replace lrpc = r(p1) if lrpc<r(p1)
	replace worker_fx_baseline = r(p1) if worker_fx_baseline<r(p1)
	
*graph 
	#delimit ;
		twoway (kdensity lrpc if Week<=0 & Treatment!=5, lw(medthick) lc(gs10)) (kdensity worker_fx_baseline if use_pre & Week<=0 & Treatment!=5, lw(medthick) lp(longdash) lc(black)), 
		xtitle("Log Revenue-per-Call") ytitle("Density") graphregion(color(white)) scale(1.2)  
		legend(label(1 "Raw Log RPC") label(2 "Agent Fixed Effects") region(color(white)));
		graph export "$figurepath\figure1.pdf", replace;
	#delimit cr
	
	

*------------------------------------------------------------------------------*
*  Figure 2: illustration of experimental design and allocation to treatments
*------------------------------------------------------------------------------*
      * illustration made elsewhere
	  
*------------------------------------------------------------------------------*
*  Figure 3: mean revenue-per-call by treatment during the pre-intervention and intervention periods
*------------------------------------------------------------------------------*

	*varibles before and during treatment
		gen rpc_pre = .
		gen rpc_treat = .
		
	*fill in graph variables
		forvalues i = 1(1) 5 {
			sum RPC if Treatment ==`i' & periodn3_0 ==1 
			replace rpc_pre = r(mean) if Treatment ==`i' & periodn3_0 ==1
			sum RPC if Treatment ==`i' & period1_4 ==1 
			replace rpc_treat = r(mean) if Treatment ==`i' & period1_4 ==1		
		}
		
	*reorder the bars external control first
		gen graph_treat = Treatment + 1  
		replace graph_treat = 1 if Treatment ==5
	
	*graph figure 2
	#delimit ;
		graph bar  rpc_pre rpc_treat, over(graph_treat,  
		relabel(1 "External Control" 2 "Internal Control" 3 "Pair-Incentives"  
		4 "Structured-Meetings" 5 "Combined")  )  
		graphregion(color(white))   ytitle("Revenue-per-Call (Normalized)")  
		legend(lab(1 "Before Study") lab(2 "Treatment Period") region(lcolor(white))  )
		scale(0.9) bar(1, fcolor(gs14) color(gs14)) bar(2, fcolor(gs1) color(gs1));
		graph export "$figurepath\figure3.pdf", replace;
	#delimit cr 

	 
*------------------------------------------------------------------------------*
*  Figure 4:  the evolution of revenue-per-call over time, by treatment group
*------------------------------------------------------------------------------*
 
	*generate variable week by treatment
		gen WeekTreat = .
		forvalues i = 1(1)5 { 
			replace WeekTreat = Week + 8 + (`i' - 1)*42 if Treatment == `i'
		}
	*create variable that is the average revenue per call for week by treatment
		bysort WeekTreat: egen graphRPC = mean(RPC)	

		
	*graph 
	#delimit ;
		twoway connected graphRPC Week if Treatment == 1 & Week < 25  , ms(Sh) lp(dash) lc(gs8)  lw(medthick) || 
		connected graphRPC Week if Treatment == 2   & Week < 25 , ms(d) mc(gs4) lc(gs4) lw(medthick) || 
		connected graphRPC Week if Treatment == 3  & Week < 25  , ms(T) mc(gs6) lc(gs6) lw(medthick)|| 
		connected graphRPC Week if Treatment == 4 & Week < 25, lc(black) mc(black) lw(medthick)  || 
		line graphRPC Week if Treatment == 5  & Week < 25,  lp(dash) lc(gs10) lw(medthick)   
		legend(lab(1 "Internal Control") lab(2 "Pair-Incentives") lab(3 "Structured-Meetings") lab(4 "Combined") lab(5 "External Control") order(3 2 4 1 5) region(color(white)) ) 
		xlab(-6(2)24) xscale(r(-7 24)) 
		graphregion(color(white)) ytitle("Revenue-per-Call (Normalized)") 
		xline(0, lc(black)) xline(4, lc(black)) ;
		graph export "$figurepath\figure4.pdf", replace;

	#delimit cr 


*------------------------------------------------------------------------------*
* Figure 5: dispersion in log revenue-per-call in the post-intervention period
*------------------------------------------------------------------------------*		
	cap drop bgwgroup
	
 	gen demean_lrpc = lrpc
	forvalues i = 1/4 {
		sum lrpc if Week>=5 & Week<=24 & Treatment==`i'
		replace demean_lrpc = lrpc-r(mean) if Treatment==`i' & Week>=5 & Week<=24 
	}
	
	sum lrpc if Week>=5 & Week<=24 & Treatment>=2 & Treatment<=4
	gen dm_active = demean_lrpc + r(mean)

	gen useTr = (Treatment==3|Treatment==4)
	bysort Treatment: sum demean_lrpc if useTr ==1 & Week >=5 & Week <=24
	  
	  
	** dm_active pools Combined + SM treatments by demeaning the individual treatments 
	** and then re-centering around the grand mean for those treatments.  
	
*graph figure 5
	#delimit ; 
		twoway (kdensity dm_active if useTr==1   & Week>=5 & Week<=24, lw(medthick)  lc(gs10)  )  
		(kdensity lrpc if (Treatment==1) & Week>=5 & Week<=24, lw(medthick)  lp(longdash) lc(black))  
		, graphregion(fcolor(gs16)) ytitle("Density") xtitle("Log Revenue-per-Call Post-Intervention")  
		scale(1.2)  
		legend(label(1 "Meetings and Combined") label(2 "Internal Control") region(color(white)));
		graph export "$figurepath\figure5.pdf", replace;
	#delimit cr 
	 
	

	
	
 log close